package com.tnsw.coreutils.files;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;

public class FileUtils {

	public static void copyFile(InputStream is, String dest) throws IOException {
		OutputStream out = new FileOutputStream(dest);
		
		byte[] buffer = new byte[1024];
		int length;
		while ((length = is.read(buffer)) > 0) {
			out.write(buffer, 0, length);
		}
		
		out.flush();
		out.close();
		is.close();
	}
	
	public static byte[] readFile(String fileName) throws FileNotFoundException, IOException {
		
		RandomAccessFile r = new RandomAccessFile(fileName, "r");
		byte[] b = new byte[(int) r.length()];
		int readLength = r.read(b, 0, (int) r.length());
		
		return(b);
	}
}
